#include"SqList.h"
void SLInit(SL* psl)
{
	assert(psl);
	psl->arr = NULL;
	psl->size = 0;
	psl->capicity = 0;
}
void SLDestroy(SL* psl)
{
	assert(psl);
	if (psl != NULL)
	{
		free(psl->arr);
		psl->arr = NULL;
		psl->size = 0;
		psl->capicity = 0;
	}
	
}
void SLPrint(SL* psl)
{
	assert(psl);
	int i = 0;
	for (i = 0; i < psl->size; i++)
	{
		printf("%d ", psl->arr[i]);
	}
	printf("\n");
}

void SLCheckCapicity(SL* psl)
{
	assert(psl);
	if (psl->size == psl->capicity)
	{
		int newcapicity = psl->capicity == 0 ? 4 : 2 * psl->capicity;
		SLDateType* tmp = (SLDateType*)realloc(psl->arr, sizeof(SLDateType) * newcapicity);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		psl->arr = tmp;
		psl->capicity = newcapicity;
	}
}
void SLPushBack(SL* psl,SLDateType x)
{
	
	SLCheckCapicity(psl);
	psl->arr[psl->size] = x;
	psl->size++;
}
void SLPushFront(SL* psl, SLDateType x)
{
	
	SLCheckCapicity(psl);
	int end = psl->size - 1;
	while (end >= 0)
	{
		psl->arr[end + 1] = psl->arr[end];
		end--;
	}
	psl->arr[0] = x;
	psl->size++;
}
void SLPopBack(SL* psl)
{
	assert(psl->size>0);
	psl->size--;
}
void SLPopFront(SL* psl)
{
	assert(psl->size > 0);
	for (int i = 1; i < psl->size;i++)
	{
		psl->arr[i-1] = psl->arr[i]; 

	}
	psl->size--;
}
void SLInsert(SL* psl, int pos, SLDateType x)
{
	assert(psl);
	SLCheckCapicity(psl);
	int end = psl->size-1;
	if (pos < psl->size && pos>0)
	{
		while (end >pos-1)
		{
			psl->arr[end+1] = psl->arr[end];
			end--;
		}
	}
	psl->arr[pos - 1] = x;
	psl->size++;
	
}
void SLErase(SL* psl, int pos, SLDateType x)
{
	assert(psl);
	int begin = pos - 1;
	while (begin <= psl->size - 1)
	{
		psl->arr[begin] = psl->arr[begin + 1];
		begin++;
	}
	psl->size--;
}
